<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>信息修改</title>
    <link th:href="@{/img/Bandit.ico}" rel="icon">
    <link th:href="@{/layui/css/layui.css}" rel="stylesheet">
    <link th:href="@{/editor.md-master/css/editormd.css}" rel="stylesheet" >
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div th:replace="~{commons/common::BackHeader}"></div>
    <div class="layui-body">
        <fieldset class="layui-elem-quote">修改信息</fieldset>
        <form class="layui-form" id="messageUpdate" style="width: 1680px">
            <!--id-->
            <div class="layui-form-item" hidden>
                <label class="layui-form-label">id</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="msgID" id="msgID" th:value="${message.msgID}">
                </div>
            </div>
            <!--标题-->
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="msgTitle" id="msgTitle" placeholder="输入文章标题" required lay-verify= "required" th:value="${message.msgTitle}" >

                </div>
            </div>
            <!--类型-->
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <select name="interest" lay-filter="type" id="typeSelect">
                        <option th:each="type,typeStat:${typeList}" th:value="${type.typeID}" th:text="${type.typeInfo}" th:selected="${message.msgType eq type.typeID}"></option>
                    </select>
                </div>
            </div>
            <!--正文-->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <fieldset>正文编辑</fieldset>
                    <br>
                    <div id="test-editor">
                        <textarea name="msgInfo" id="msgInfo" style="display: none" th:text="${message.msgInfo}"></textarea>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit id="save_btn">点击修改</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/editor.md-master/editormd.js}"></script>
<script type="text/javascript">
    var prefix = "/back/message";
    $(function() {
        var editor = editormd("test-editor", {
            path: "/editor.md-master/lib/",
            width: "100%",
            height: 600,
            emoji: true,
            tocm: true,
            tex: true,
            dialogLockScreen: false,
            dialogShowMask: false,
            dialogDraggable: false,
            dialogMaskOpacity: 0.4,
            dialogMaskBgColor: "#000",
            imageUpload: true,
            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL: prefix + "/uploadImage",  //需要修改为后端的解析图片上传的url
            saveHTMLToTextarea: true,
            onload: function () {
                initPasteDragImg(this); //允许粘贴和拖拉图片到editormd
            }
        });

        $('#messageUpdate').on('submit', function (e){
            let layer;
            layui.use(['layer'],function(){
                layer = layui.layer;
                layer.load(1);
            })
            e.preventDefault();
            let Data = $(this).serializeArray();
            let htmlText = editor.getHTML();
            let md = editor.getMarkdown();
            let content = htmlText + '￥' + md;
            console.log(Data);
            console.log(content);
            $.ajax({
                type: 'POST',
                url: prefix + '/editConfirm',
                data: {
                    "msgID":Data[0].value,
                    "msgTitle":Data[1].value,
                    "msgType":Data[2].value,
                    "msgInfo":Data[3].value,
                    "test-editor-html-code":Data[4].value,
                },
                dataType: 'json',
                success: function (res) {
                    console.log(res.msg);
                    parent.layer.closeAll();
                    layer.msg('修改成功！');
                    setTimeout(function(){
                        parent.layer.closeAll();
                        window.location.href = prefix + '/list';
                    },2000)
                },
                error: function () {
                    console.log("错误");
                }
            })
        });

    });
</script>
<script th:replace="~{commons/common::Editor_Img}"></script>
</body>
</html>